<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Login extends SiteController {

    public function logout() {

        $this->load->library('facebook');

        // Logs off session from website
        $this->facebook->destroySession();
        unset($_SESSION[USER_FB]);
        redirect('site/home/index');
    }

    public function autenticar() {
        if ($_SERVER ['REQUEST_METHOD'] == 'GET' && isset($_GET ['code'])) {
            $code = urlencode($this->input->get('code'));
            $redirect_uri = site_url('site/login/autenticar');

            $token_url = "https://graph.facebook.com/oauth/access_token?client_id=" . APP_ID . "&redirect_uri=" . $redirect_uri . "&client_secret=" . APP_SECRET . "&code=" . $code;
            $response =  file_get_contents($token_url);
            if ($response) {
                $params = null;
                parse_str($response, $params);
                if (isset($params ['access_token']) && $params ['access_token']) {
                    $graph_url = "https://graph.facebook.com/me?access_token=" . $params ['access_token'];
                    $user = json_decode(file_get_contents($graph_url));
                    $_SESSION[USER_FB] = $user;
                    redirect('site/home/index');
                } else {
                    echo "Erro de conexão com Facebook";
                    redirect('site/home/index');
                }
            } else {
                echo "Erro de conexão com Facebook";
                redirect('site/home/index');
            }
        } else if ($_SERVER ['REQUEST_METHOD'] == 'GET' && isset($_GET ['error'])) {
            echo 'Permissão não concedida';
        }
    }

}

/* End of file login.php */
/* Location: ./application/controllers/site/login.php */